<template>
  <div></div>
</template>

<script setup>
import { Base64 } from "js-base64";
import { getQueryObject } from "@/utils";
import { ElLoading } from "element-plus";
import useUserStore from "@/store/modules/user";
import { onMounted } from "vue";

const router = useRouter();
const ssoLogin = () => {
  const { u, p, app } = getQueryObject();
  if (u && p) {
    const loading = ElLoading.service({
      lock: true,
      text: "正在登录...",
      background: "rgba(0, 0, 0, 0.7)",
    });
    if (useUserStore().roles.length === 0) {
      const params = {
        username: Base64.decode(u),
        password: Base64.decode(p),
      };
      useUserStore()
        .login(params)
        .then(() => {
          if (app) {
            router.push({
              path: `/${app}`,
              query: { from: "sso" },
            });
          } else router.push({ path: `/` });
        })
        .catch(() => {
          router.push({ path: "/login" });
        })
        .finally(() => {
          loading.close();
        });
    } else {
      console.log({ app });
      router.push({
        path: `/${app}`,
      });
    }
  } else {
    router.push({ path: "/login" });
  }
};
onMounted(() => {
  console.log("111111111111111111111");
  ssoLogin();
});
</script>

<style lang="scss" scoped></style>
